import Homework from "../models/homeworkModel.js";
import Course from "../models/courseModel.js";
import CourseSelection from "../models/courseSelectionModel.js";

export default class HomeworkController {
    static async apiGetHomework(req, res, next) {
        try {
            // query all homework for course(category == 'question')
            const course = req.query.course;
            const data = await Homework.find({course: course, category: 'question'});
            console.log("get homework data:", data);
            res.status(200).json({ homework: data});
        } catch(e) {
            console.log(e);
            res.status(500).json({ error: e.message });
        }
    }

    static async apiAddHomework(req, res, next) {
        try {
            const {name, content, uploader, course} = req.body;
            const hwk = await Homework.findOne({
                name: name,
                uploader: uploader,
                course: course,
                category: "question"
            });
            console.log("apiAddHomework course: ", course);
            if (hwk) {
                hwk.content = content;
                await hwk.save();
                res.status(200).json({ message: "update home work successfully"});
            } else {
                const newHomework = new Homework({
                    name,
                    content,
                    uploader,
                    course,
                    category: "question"
                });
                console.log("newHomework structure: ", newHomework);
                await newHomework.save();
                res.status(200).json({ message: "add home work successfully"});
            }
        } catch(e) {
            console.log(e);
            res.status(500).json({ error: e.message });
        }
    }

    static async apiGetReportByStudent(req, res, next) {
        try {
            const {name, uploader, course} = req.body;
            const hwk = await Homework.findOne({
                name: name,
                uploader: uploader,
                course: course,
                category: "answer"
            });
            if (hwk) {
                res.status(200).json(hwk);
            } else {
                res.status(200).json();
            }
        } catch(e) {
            res.status(500).json({ error: e.message });
        }
    }

    static async apiSubmitHomework(req, res, next) {
        try {
            const {name, content, uploader, course} = req.body;
            const hwk = await Homework.findOne({
                name: name,
                uploader: uploader,
                course: course,
                category: "answer"
            });
            if (hwk) {
                hwk.content = content;
                await hwk.save();
                res.status(200).json({ message: "update home work successfully"});
            } else {
                const newHomework = new Homework({
                    name,
                    content,
                    uploader,
                    course,
                    category: "answer"
                });
                console.log("newHomework structure: ", newHomework);
                await newHomework.save();
                res.status(200).json({ message: "submit homework succesfully"});
            }
        } catch(e) {
            res.status(500).json({ error: e.message });
        }
    }

    static async apiScoreHomework(req, res, next) {
        try {
            const {id, score} = req.body;
            const hwk = await Homework.findById(id);
            hwk.score = score;
            await hwk.save();
            res.status(200).json({ message: "score homework succesfully"});
        } catch(e) {
            console.log(e);
            res.status(500).json({ error: e.message });
        }
    }

    static async apiGetReportsByHomeworkId(req, res, next) {
        try {
            const {id} = req.body;
            const homework = await Homework.findById(id);
            const data = await CourseSelection
                .find({course: homework.course})
                .populate('student');
            const students = data.map(item => {return item.student});
            var reports = await Homework
                .find({name: homework.name, category: 'answer', course: homework.course})
                .populate('uploader');
            const inlist = reports.map(item => {return item.uploader._id});
            const notin = [];
            students.forEach(item => {
                if (!inlist.includes(item._id)) {
                    notin.push({
                        uploader: item,
                        score: 0,
                        content: '',
                        name: homework.name,
                        category: 'answer',
                    });
                }
            });
            reports.push(...notin);
            res.status(200).json(reports);
        } catch(e) {
            console.log("error in getReportsByHomeworkId", e);
            res.status(500).json({ error: e.message });
        }
    }
}
